package com.hdaccp.gymxmjpa.Controller;


import com.hdaccp.gymxmjpa.Dao.MenberDao;
import com.hdaccp.gymxmjpa.Dao.chongzhiDao;
import com.hdaccp.gymxmjpa.Service.MenberDaoImpl;
import com.hdaccp.gymxmjpa.entity.Chongzhi;
import com.hdaccp.gymxmjpa.entity.Member;
import com.hdaccp.gymxmjpa.entity.Membertype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/cz")
public class chongzhiController {

    @Autowired
    private chongzhiDao chongzhiDao;

    @Autowired
    private MenberDao menberDao;

    @Autowired
    private MenberDaoImpl menberDaoImpl;

    @PersistenceContext
    private EntityManager entityManager;

    @RequestMapping("/jin")
    public String jin(){
        return "WEB-INF/jsp/HYMemberjilu";
    }



    @RequestMapping("/jin2")
    public String jin2(){
        return "WEB-INF/jsp/ShouRuTongji";
    }

    @RequestMapping("/xin")
    @ResponseBody
   public Map<String,Object> cye(Chongzhi chongzhi){

        //充值money
        Member member=menberDao.findById(chongzhi.getMember().getMemberId()).get();
        member.setMemberbalance(member.getMemberbalance()+chongzhi.getMoney());
        menberDao.save(member);

        //添加充值记录
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Timestamp dat=java.sql.Timestamp.valueOf(df.format(new Date()));
        chongzhi.setDate(dat);
        chongzhi.setCzStatic(1L);
        chongzhiDao.save(chongzhi);
        Map<String,Object>  map1=new HashMap<String,Object>();
        map1.put("hyname",null);
        map1.put("ktype",0);
        map1.put("qi",1);
        map1.put("shi",5);
       return menberDaoImpl.query2(map1);
   }


    @RequestMapping("/query")
    @ResponseBody
   public Map<String,Object> query(int pageSize,int pageNumber,String xdate,String ddate){

        Pageable pageable=new PageRequest(pageNumber-1,pageSize);
        Specification<Chongzhi> specification=new Specification<Chongzhi>() {
            @Override
            public Predicate toPredicate(Root<Chongzhi> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder)  {
                List<Predicate> list=new ArrayList<Predicate>();

                if ((xdate != null && !xdate.equals("")) || (ddate!=null && !ddate.equals(""))){
                    list.add(criteriaBuilder.between(root.get("date"),xdate,ddate));
                }

                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
        Page<Chongzhi> list1 = chongzhiDao.findAll(specification,pageable);
        List<Chongzhi> li = list1.getContent();
        Map<String,Object> map = new HashMap();
        map.put("total",list1.getTotalElements());
        map.put("rows",li);
        return map;
   }

    @RequestMapping("/tongji")
   public int[] TOngji(){
        String[] array={"2018-01","2018-02","2018-03","2018-04","2018-05","2018-06","2018-07","2018-08","2018-09","2018-10","2018-11","2018-12"};
        int[] intar=new int[12];
        for (int i=0;i<array.length;i++){
            String jpa="SUM(a.money) from ChongZhi as a where `Date` like('%"+array[i]+"%')";
            Query query=entityManager.createQuery(jpa);
            intar[i]=(Integer)query.getSingleResult();
            System.out.println(intar[i]);
        }
        return intar;
   }
}
